package com.food.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


import com.food.dao.in.GradeDAO;
import com.food.model.Grade;

public class GradeDAOJdbc implements GradeDAO {
//	private static final String URL = "jdbc:sqlserver://192.168.34.108:1433;database=PJDB";
//	private static final String USER = "sa";
//	private static final String PASSWORD = "sa123456";
	
	private DataSource ds = null;
	public GradeDAOJdbc() {
		try {
			Context ctx = new InitialContext();
			this.ds = (DataSource) ctx.lookup("java:comp/env/jdbc/food");
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	
	private static final String INSERT = 
			"insert into grade (mbid, rid, gscore) values(?,?,?)";
	@Override
	public void insertGrade(Grade bean) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(INSERT);
			stmt.setInt(1, bean.getMbid());
			stmt.setInt(2, bean.getRid());
			stmt.setInt(3, bean.getGscore());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
		if (stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	  }
	}
	private static final String SELECT=
			"select gid,mbid, rid, gscore from grade where rid=?";
	@Override
	public List<Grade> findGradeByRecipe(int rid) {
		List<Grade> result = null;
		Grade fgrade = null;
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rest = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(SELECT);
			stmt.setInt(1,rid);
			rest = stmt.executeQuery();
			result=new ArrayList<Grade>();
			while(rest.next()){
				fgrade = new Grade();
				fgrade.setGid(rest.getInt("gid"));
				fgrade.setMbid(rest.getInt("mbid"));
				fgrade.setRid(rest.getInt("rid"));
				fgrade.setGscore(rest.getInt("gscore"));
				result.add(fgrade);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
		if (rest!=null) {
			try {
				rest.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		 }
	  }
		return result;
	}
	public static void main(String args[]){
//		GradeDAOJdbc la = new GradeDAOJdbc();
//		Grade gr = new Grade();
//		gr.setMbid(2);
//		gr.setRid(1);
//		gr.setGscore(110);
//		la.insertGrade(gr);
		
		
//		List<Grade> l = new ArrayList<Grade>();
//		l = la.findGradeByRecipe(1);
//		System.out.println(l);
		
	}
}
